package com.xqboss.apps.service.equipment;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xqboss.apps.convert.equipment.EquipmentAttrPointRatioConverter;
import com.xqboss.apps.domain.equipment.EquipmentAttrPointRatio;
import com.xqboss.apps.domain.equipment.EquipmentAttrTemplate;
import com.xqboss.apps.dto.equipment.EquipmentAttrPointRatioDto;
import com.xqboss.apps.mapper.equipment.EquipmentAttrPointRatioMapper;
import com.xqboss.apps.mapper.equipment.EquipmentAttrTemplateMapper;
import com.xqboss.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Trent
 * @date 2023/8/16
 */
@Service
public class EquipmentAttrPointRatioService extends ServiceImpl<EquipmentAttrPointRatioMapper, EquipmentAttrPointRatio> {

    @Autowired
    private EquipmentAttrPointRatioConverter equipmentAttrPointRatioConverter;

    /**
     * 根据装备属性ID删除爆率信息
     * @param equipmentAttrTemplateId 装备属性ID
     */
    public void removeByTemplateId(Long equipmentAttrTemplateId) {
        remove(new LambdaQueryWrapper<EquipmentAttrPointRatio>()
                .eq(EquipmentAttrPointRatio::getEquipmentAttrTemplateId, equipmentAttrTemplateId));
    }

    /**
     * 保存装备属性点数爆率信息
     * @param pointRatioListDto 装备属性ID爆率
     */
    @Transactional(rollbackFor = Exception.class)
    public void save(Long equipmentAttrTemplateId, List<EquipmentAttrPointRatioDto> pointRatioListDto) {
        List<EquipmentAttrPointRatio> pointRatioList = equipmentAttrPointRatioConverter.fromDtoList(pointRatioListDto);
        for (EquipmentAttrPointRatio ratio : pointRatioList) {
            ratio.setEquipmentAttrTemplateId(equipmentAttrTemplateId);
        }
        boolean result = saveBatch(pointRatioList);
        if(!result){
            throw new ServiceException("保存失败！");
        }
    }

    /**
     * 根据模板ID获取爆率配置
     * @param attrTempId
     * @return
     */
    public List<EquipmentAttrPointRatio> getByTemplateId(Long attrTempId){
        return list(new LambdaQueryWrapper<EquipmentAttrPointRatio>().eq(EquipmentAttrPointRatio::getEquipmentAttrTemplateId, attrTempId));
    }
}
